<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws.
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.).
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations).
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Gallery/classes/PhpFox_ComponentUploadImage.class.php, $Author:  $ */



/** Component for uploading user image into gallery
 *
 * @version $Id: PhpFox_ComponentUploadImage.class.php 465 2008-06-05 16:02:34Z phpfoxsv $
 * @package module.gallery
 */
class PhpFox_Mod_Gallery_ComponentUploadImage extends BaseComponent
{
    function process()
    {
        //initialization
        $oGalleryService = &$this->_oModule->getService('Gallery');
        /* @var $oGalleryService PhpFox_Mod_Gallery_ServiceGallery */
        $oGalleryItem = &$this->_oModule->getItem('Gallery');
        /* @var $oGalleryItem PhpFox_Mod_Gallery_ItemGallery */
        $oSecurityService = &App::getModuleService('Account', 'Security');
        /* @var $oSecurityService PhpFox_Mod_Account_ServiceSecurity */
        $oAccountService = &App::getModuleService('Account', 'Account');
        /* @var $oAccountService PhpFox_Mod_Account_ServiceAccount */
        $sCurrentUserLogin = $oSecurityService->getCurrentUserLogin($oSecurityService->getCurrentUser());
        $oReq = &App::getRequest();
        $oTpl = &$this->_oModule->getTemplate();
        App::loadClass('file/Image');
        $oImage = &new file_Image();
		App::loadClass('Text');
        $oTxt = &new Text();

        $oSecurityService->checkPerm('gallery', 'full');
        $oSecurityService->checkAccess('gallery');
        $iUsedSpace = $oAccountService->getUsedSpace($oSecurityService->getCurrentUserLogin());
        $iAllowedSpace = $oAccountService->getAllowedSpace($oSecurityService->getCurrentUser());
        $iMaxUploadSize = App::getSetting('max_upload_size') * 1024;
        if (($iAllowedSpace != 0) && (($iAllowedSpace - $iUsedSpace) < $iMaxUploadSize))
        {
            $iMaxUploadSize = $iAllowedSpace - $iUsedSpace;
        }
        if ($oReq->get('uploaded'))
        {
       $iImgsize=filesize(App::getParam('path').'file/pic/gallery/'.$oReq->get('uploaded').'_view.jpg');
			if($iImgsize==0)
			{
				App::gotoUrl('account.myitems.edit-pic');
			}
			else
			{            
		        $aImageData = $this->oDatabase->getRow('
		        	SELECT user_id
		        	FROM '. App::getT('main') .'		        		
		        	WHERE id = ' . (int) $oReq->get('uploaded') . '
		        ');				
		        if ($aImageData['user_id'] != App::getUser('id'))
		        {
		        	App::gotoUrl('account.myitems.edit-pic');
		        }				
			$oTpl->assign(array(
                'sImageUrl' => App::getParam('gallery.pic.url').$oReq->get('uploaded').'_view.jpg',
                'bUploaded' => 1,
            ));
			}
        }
        elseif ($oReq->getAction() == 'upload')
        {
            if (!array_key_exists('image', $_FILES))
            {
                $this->addErrors('Mod_Gallery.error.noimage');
            }
            if (!count(App::getErrors()))
            {
                if ($iAllowedSpace)
                {
                    $oImage->aLimits['FILE_MAX_SIZE'] = $iMaxUploadSize;
                }
                $oGalleryItem->aData = array
                (
                    'user_id'      => App::getUser('id'),
                    'text'      => htmlspecialchars($oReq->get('title')),
                    'time'      => time(),
                    'category' => $oReq->get('category'),
                    'allow'     => App::getSetting('approve_images') ? '1' : '',
                	'who_view'  => $oReq->get('whoview'),
                	'tags'      => strip_tags($oTxt->preParseText($oTxt->ShortenText($oReq->get('tags'), 75)))
                );
                $iImageId = $oGalleryItem->insert();
                $oAccountService->changeUserInfo($oSecurityService->getCurrentUser(), 'gallery');
                $sDestPath = App::getParam('gallery.pic.path');
                if ($oImage->upload('image', $sDestPath, $iImageId, 'jpg'))
                {
                    $oImage->loadInfo($sDestPath.$iImageId.'.jpg');
                    $oImage->_checkLimits();
                }
                $this->addErrors($oImage->getErrors());

                if (!count($this->getErrors()))
                {
                    if (App::getSetting('gd_watermark'))
                    {
                        if (App::getSetting('gd_watermark_image'))
                        {
                            $sWatermarkPath = App::getParam('watermark.path');
                            $sWImage = App::getSetting('gd_watermark_image_location');
                            if ($sWImage = basename($sWImage))
                            {
                                $sWatermarkPath .= $sWImage;
                                $oImage->addWatermark($sWatermarkPath);
                            }
                        }
                        else
                        {
                            if ($sWText = App::getSetting('gd_watermark_text'))
                            {
                                $oImage->addText($sWText, 12, App::getSetting('gd_watermark_color'));
                            }
                        }
                    }                    
                    $oImage->makeThumbnail($sDestPath.'thumb/'.$iImageId.'.jpg', App::getSetting('upload_set_size_thumb'), App::getSetting('upload_set_size_thumb'));                    
              
                    if (App::getSetting('gd_watermark'))
                    {
                        if (App::getSetting('gd_watermark_image'))
                        {
                            $sWatermarkPath = App::getParam('watermark.path');
                            $sWImage = App::getSetting('gd_watermark_image_location');
                            if ($sWImage = basename($sWImage))
                            {
                                $sWatermarkPath .= $sWImage;
                                $oImage->addWatermark($sWatermarkPath);
                            }
                        }
                        else
                        {
                            if ($sWText = App::getSetting('gd_watermark_text'))
                            {
                                $oImage->addText($sWText, 12, App::getSetting('gd_watermark_color'));
                            }
                        }
                    }
                    $oImage->makeThumbnail($sDestPath . $iImageId.'_view.jpg', App::getParam('upload_set_size_view'), App::getParam('upload_set_size_view'));                    
                    
                    if(!App::getParam('bSaveOriginalImages'))
                    {
                    	unlink($sDestPath . $iImageId.'.jpg');
                    }
                    App::gotoUrl('public.account.submit.upload-pic', array('uploaded' => $iImageId));
                }
                else
                {
                    $oGalleryService->deleteItem($iImageId);
                }
            }
        }

        $oTpl->assign(array(
        	'aPrivacySettings' => $oAccountService->getPrivacySettings(),
            'aCategories'    => App::getOptions('image_cat'),
            'sMaxFileSize'   => floor($iMaxUploadSize / 1024),
            'aErrors'        => $this->getErrors(),
            'bApprove'       => App::getParam('approve_images'),
            'iUsedSpace'     => $iUsedSpace,
            'iAllowedSpace'  => $iAllowedSpace,
            'iMaxUploadSize' => $iMaxUploadSize,
            'aData'          => $oReq->getAll(),
        ));
        return $oTpl->fetch('UploadImageForm.html');
    }
}
?>